package org.tlipoca.smarticlefavorites.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.GatewayConstant;
import org.genntii.smcommon.result.Result;
import org.genntii.smdomain.dto.ArticleFavoritesDTO;
import org.genntii.smdomain.vo.ArticleFavoritesCountVO;
import org.genntii.smdomain.vo.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tlipoca.smarticlefavorites.service.ArticleFavoritesService;

import java.util.ArrayList;

@Tag(name = "/article_favorites")
@Slf4j
@RestController
@RequestMapping("/article_favorites")
public class ArticleFavoritesController {

    @Autowired
    private ArticleFavoritesService articleFavoritesService;

    @Operation(summary = "根据收藏夹查询文章收藏")
    @GetMapping("/{directoryId}")
    public Result<ArrayList<ArticleVO>> getArticleFavoritesByDirectoryId(@PathVariable Long directoryId) {
        return Result.success(articleFavoritesService.getArticleFavoritesByDirectoryIdService(directoryId));
    }

    @Operation(summary = "查询用户所有收藏夹下收藏文章数")
    @GetMapping
    public Result<ArrayList<ArticleFavoritesCountVO>> getArticleFavoritesCountByUserIdGroupByDirectoryId(HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        return Result.success(articleFavoritesService.getArticleFavoritesCountByUserIdGroupByDirectoryIdService(userId));
    }

    @Operation(summary = "新增文章收藏")
    @PostMapping
    public Result addArticleFavorites(@RequestBody ArticleFavoritesDTO articleFavoritesDTO, HttpServletRequest request) {
        Long userId = Long. valueOf(request.getHeader(GatewayConstant.USER_ID));
        articleFavoritesService.addArticleFavoritesService(articleFavoritesDTO,userId);
        return Result.success();
    }

    @Operation(summary = "删除文章收藏")
    @DeleteMapping("/{favoritesId}")
    public Result deleteArticleFavorites(@PathVariable Long favoritesId) {
        articleFavoritesService.deleteArticleFavoritesService(favoritesId);
        return Result.success();
    }

    @Operation(summary = "批量删除文章收藏")
    @DeleteMapping
    public Result deleteArticleFavoritesBatch(@RequestParam("favoritesIds") ArrayList<Long> favoritesIds) {
        articleFavoritesService.deleteArticleFavoritesBatchService(favoritesIds);
        return Result.success();
    }
}

